﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class FamilyForm : Form
    {
        int worthyId = -1;
        ModelContainer ctx = new ModelContainer();
        Form fo = null;
        public bool is_loaded = false;
        public FamilyForm(int wId,Form m)
        {
            fo = m;
            this.MdiParent = m;
            worthyId = wId;
            InitializeComponent();
        }
        Person person = new Person();
        private void FamilyForm_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            if (worthyId > 0)
            {
                Worthy w = ctx.Worthies.Single(c => c.Id == worthyId);
                Person p = ctx.People.Single(s => s.Id == w.PersonId);
                person = p;
                txb_worthy.Text = p.FirstName + " " + p.FatherName + " " + p.LastName;
                txb_WorthyId.Text = worthyId.ToString();
                personBindingSource.DataSource = ctx.People.Where(pe => pe.WorthyId == worthyId).ToList();
                dgv_persons.DataSource = personBindingSource;
                // add this form to window list
                ((Forms.FormMDI)fo).addFormToWindowsList(this);
            }
        }

        private void dgv_persons_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            int id = -1;
            if (dgv_persons.Rows[e.RowIndex].Cells[0].Value != null)
            {
                id = (int)dgv_persons.Rows[e.RowIndex].Cells[0].Value;
            }
            if (id > 0)
            {
                try
                {
                    Person p = ctx.People.Single(c => c.Id == id);
                    if (dgv_persons.Rows[e.RowIndex].Cells[1].Value != null)
                    {
                        p.FirstName = dgv_persons.Rows[e.RowIndex].Cells[1].Value.ToString();
                        try 
                        {	        
                    		if (dgv_persons.Rows[e.RowIndex].Cells[6].Value != null)
                            if (dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString().Length > 0)
                            {
                                int dobi;
                                DateTime dobD;
                                if (int.TryParse(dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString(), out dobi))
                                {
                                    p.DOB = DateTime.Now.AddYears(-dobi);
                                }
                                else if (DateTime.TryParse(dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString(), out dobD))
                                {
                                    p.DOB = dobD;
                                }
                            }
                        }
	                    catch (Exception)
	                    {
        		                      MessageBox.Show(@"يجب أن يدخل التاريخ بصيغة يوم\شهر\سنة");
	                    }
                        
                        if (dgv_persons.Rows[e.RowIndex].Cells[7].Value != null)
                            p.NationalNumber = dgv_persons.Rows[e.RowIndex].Cells[7].Value.ToString();
                        if (dgv_persons.Rows[e.RowIndex].Cells[10].Value != null)
                            p.Sex = dgv_persons.Rows[e.RowIndex].Cells[10].Value.ToString();
                        if (dgv_persons.Rows[e.RowIndex].Cells[11].Value != null)
                            p.JobTitle = dgv_persons.Rows[e.RowIndex].Cells[11].Value.ToString();
                        if (dgv_persons.Rows[e.RowIndex].Cells[12].Value != null)
                            p.HealthStatus = dgv_persons.Rows[e.RowIndex].Cells[12].Value.ToString();
                        if (dgv_persons.Rows[e.RowIndex].Cells[16].Value != null)
                            p.RelationshipWithWorthy = dgv_persons.Rows[e.RowIndex].Cells[16].Value.ToString();
                        ctx.SaveChanges();
                    }
                }
                catch
                {
                }
            }
            else
            {
                Person pe = new Person();
                if (dgv_persons.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    pe.FirstName = dgv_persons.Rows[e.RowIndex].Cells[1].Value.ToString();
                    try
                    {
                        if (dgv_persons.Rows[e.RowIndex].Cells[6].Value != null)
                            if (dgv_persons.Rows[e.RowIndex].Cells[6].Value != null)
                                if (dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString().Length > 0)
                                {
                                    int dobi;
                                    DateTime dobD;
                                    if (int.TryParse(dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString(), out dobi))
                                    {
                                        pe.DOB = DateTime.Now.AddYears(-dobi);
                                    }
                                    else if (DateTime.TryParse(dgv_persons.Rows[e.RowIndex].Cells[6].Value.ToString(), out dobD))
                                    {
                                        pe.DOB = dobD;
                                    }
                                }
                    }
                    catch (Exception)
                    {

                        MessageBox.Show(@"يجب أن يدخل التاريخ بصيغة يوم\شهر\سنة");
                    }
                    
                    if (dgv_persons.Rows[e.RowIndex].Cells[7].Value != null)
                        pe.NationalNumber = dgv_persons.Rows[e.RowIndex].Cells[7].Value.ToString();
                    if (dgv_persons.Rows[e.RowIndex].Cells[10].Value != null)
                        pe.Sex = dgv_persons.Rows[e.RowIndex].Cells[10].Value.ToString();
                    if (dgv_persons.Rows[e.RowIndex].Cells[11].Value != null)
                        pe.JobTitle = dgv_persons.Rows[e.RowIndex].Cells[11].Value.ToString();
                    if (dgv_persons.Rows[e.RowIndex].Cells[12].Value != null)
                        pe.HealthStatus = dgv_persons.Rows[e.RowIndex].Cells[12].Value.ToString();
                    if (dgv_persons.Rows[e.RowIndex].Cells[16].Value != null)
                        pe.RelationshipWithWorthy = dgv_persons.Rows[e.RowIndex].Cells[16].Value.ToString();
                    pe.WorthyId = worthyId;
                    ctx.People.Add(pe);
                    ctx.SaveChanges();
                }
            }
        }




        private void btn_delete_Click(object sender, EventArgs e)
        {

            try
            {
                foreach (DataGridViewRow item in this.dgv_persons.SelectedRows)
                {
                    if (dgv_persons.Rows[item.Index].Cells[0].Value != null)
                    {
                        int id = (int)dgv_persons.Rows[item.Index].Cells[0].Value;
                        Person p = ctx.People.Single(c => c.Id == id);
                        if (p.Id != person.Id)
                        {
                            DialogResult result = MessageBox.Show(" :هل أنت متأكد من حذف  "+p.FirstName, "تاكيد الحذف", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                ctx.People.Remove(p);
                                dgv_persons.Rows.RemoveAt(item.Index);
                            }
                        }
                        ctx.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void dgv_persons_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (e.ColumnIndex==1)
            {
                int val=0;
                int.TryParse(dgv_persons.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(),out val);
                if (val > 0)
                {
                    DateTime dt= DateTime.Now.AddYears(-val);
                    dgv_persons.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dt.ToShortDateString();
                }
            }
        }

        private void FamilyForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void FamilyForm_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}

    